﻿#ifndef QTERMINALSCREEN_H
#define QTERMINALSCREEN_H
#include "qTerminalChar.h"
#include <QFont>
#include <QObject>
#include <QRect>
#include <QPoint>
#include <QTimer>
typedef QVector<qTerminalChar> _lineChars;

   

class qTerminalScreen: public QObject
{
    Q_OBJECT
public:
    qTerminalScreen();
    ~qTerminalScreen();
    bool timeStamp=false;
    uint32_t scrollChangedValue=0;
    void getLineNumContent(int row) const;
    int getLeftMargin() const;
    void replace(char ch, quint32 row, int column,qTerminalFont &terminalFont);
    void setRow(quint32 value){
        this->row=value;
        if(value>this->rowMaxCount)
            this->rowMaxCount = value;
    }
    void setLineContentNum(bool flag=false)
    {
        timeStamp = flag;
    }
    void setCol(quint32 value){
        this->col = value;
    }
    quint32 getRow() const
    {
        quint32 i = 0;
        if(this->saveCursorY==0)
            i=this->row;
        else
            i = this->saveCursorY;
        return this->row;
    }
    quint32 getCol() const
    {
        return this->col;
    }
    void setTop(quint32 va) 
    {
        this->screen_top = va;
    }
    quint32 getTop() const
    {
        return this->screen_top;
    }
    void setBottom(quint32 va) 
    {
        this->screen_bottom = va % this->getScreenBufSize();
    }
    quint32 getBottom() const
    {
        return this->screen_bottom;
    }
    void setScreenRange(quint32 va) 
    {
        this->screen_range = va;
    }
    quint32 getScreenRange() const
    {
        return this->screen_range;
    }
    bool getWheelFlag() const
    {
        return this->wheelFlag;
    }
    bool getWheelShowFlag() const
    {
        return this->scroll_showflag;
    }
    void setWheelFlag(bool value) 
    {
        this->wheelFlag=value;
    }
    quint32 getRowMax() const
    {
        return this->rowMaxCount;
    }
    _lineChars* getLineContent() 
    {
        return this->lineContent;
    }
    quint32 getFontHeight() const
    {
        return this->fontHeight;
    }
    quint32 getFontWidth() const
    {
        return this->fontWidth;
    }
    QFont getFont() 
    {
        return this->font;
    }
    void setColMax(quint32 value) 
    {
        this->col_max = value;
    }
    quint32 getColMax() const
    {
        return this->col_max+getLeftMargin();
    }
    void setScrollBottom(quint32 value) 
    {
        this->scroll_bottom = value;
    }
    quint32 getScrollBottom() const
    {
        return this->scroll_bottom;
    }
    void setScrollCurrent(quint32 value) 
    {
        if(value>this->scroll_bottom)
            value = this->scroll_bottom;
        this->scroll_current = value;
    }
    quint32 getScrollCurrent() const
    {
        return this->scroll_current;
    }
    quint32 getScreenBufSize() const
    {
        return this->screenBufSize;
    }
    void setScreenBufSize(quint32 value) 
    {
        this->screenBufSize = value;
    }
    void saveCursor()//保存实际位置
    {
        this->saveCursorY = this->getRow();
        this->saveCursorX = this->getCol();
    }
    void restoreCursor()
    {
        this->setRow(this->saveCursorY);
        this->setCol(this->saveCursorX);
    }
    quint32 getScreenCursorX()//获取相对显示区域的光标位置
    {
        return this->getCol()+1;
    }
    quint32 getScreenCursorY()//获取相对显示区域的光标位置
    {
        return (this->getRow()-this->getTop()+1)%this->screenBufSize;
    }
    quint32 getSaveCursorX()//获取实际位置
    {
        return this->saveCursorX;
    }
    quint32 getSaveCursorY()//获取实际位置
    {
        return this->saveCursorY;
    }
    void setCursorY(quint32 value) 
    {
        this->cursorY = value;
    }
    quint32 getCursorY() const
    {
        return this->cursorY;
    }
    void setCursorX(quint32 value) 
    {
        this->cursorX = value;
    }
    quint32 getCursorX() const
    {
        return this->cursorX;
    }
    QPoint cursorPosition() const;//光标位置
    void scrollHandle(int tmp);
    void clearToEnd(quint32 row, int column);
    void clearScreen(quint32 row);
    QFont font;
private:
    _lineChars *lineContent=nullptr;
    
    int fontHeight;
    int fontWidth;
    bool scroll_showflag=false;//标记是否显示滚动条
    quint32 scroll_bottom=0;//记录总行数
    quint32 scroll_current=0;//当前滚动条显示位置
    quint32 screen_top=0;//显示顶部行
    quint32 screen_bottom=0;//显示底部行
    quint32 screen_range=0;//显示行数
    quint32 screenBufSize=0;//存储数据的缓冲区
    quint32 col_max=0;//存储数据的缓冲区
    quint32 row_count=0;//记录当前的总行数
    quint32 save_row=0;
    quint32 save_col=0;
    quint32 row=0,col=0;//光标位置
    int top=0,bottom=0;//终端屏幕的上下行数
    
    quint32 rowMaxCount=0;//最大的行号，设置退出keypadmode时光标的行位置

    bool row_over=false;//标记当前收到的行数是否溢出ROW_MAX
    bool wheelFlag=false;//允许更新显示内容
    quint32 run_overCount=0;
    quint32 saveCursorX=0;
    quint32 saveCursorY=0;
    int cursorX=0,cursorY=0;
    

    QTimer timer;
    char date[30];
signals:
    void setScrollValue(int min,int max,int page,int value);//设置滚动条参数
    void setScrollBarPos(int value);//鼠标滚轮更新滚动条位置
    void updateSignal();
};

#endif // QTERMINALSCREEN_H
